<template>
  <div ref="branchHospitalForm" style="position: relative;">
    <el-form :model="branchHospitalForm" :rules="pattern">
      <el-form-item label="医院名称:" prop="hospitalName" style="margin-bottom: 22px">
        <el-col :span="20" class="hospitalForm">
          <el-input
            v-model="branchHospitalForm.hospitalName"
            style="width: 400px"
            maxlength="20"
            minlength="1"
            placeholder="输入医院名称"
          />
        </el-col>
      </el-form-item>
      <el-form-item label="医院地址：" required style="margin-bottom: 22px">
        <el-col :span="20" class="hospitalForm">
          <el-col :span="24" style="display: flex">
            <el-form-item prop="provinceCode" style="padding-left: 0" class="time">
              <el-select
                ref="province"
                v-model="branchHospitalForm.provinceCode"
                @change="handelProvince"
              >
                <el-option
                  v-for="(item, index) in pca.province"
                  :key="index"
                  :value="item.code"
                  :label="item.name"
                /> </el-select></el-form-item>
            <el-form-item prop="cityCode" class="time">
              <el-select
                v-model="branchHospitalForm.cityCode"
                @change="handelCity"
              >
                <el-option
                  v-for="(item, index) in pca.forCity"
                  :key="index"
                  :value="item.code"
                  :label="item.name"
                /> </el-select></el-form-item>
            <el-form-item prop="areaCode" class="time">
              <el-select
                v-model="branchHospitalForm.areaCode"
                @change="handelArea"
              >
                <el-option
                  v-for="(item, index) in pca.forArea"
                  :key="index"
                  :value="item.code"
                  :label="item.name"
                /> </el-select></el-form-item>
          </el-col>
        </el-col>
      </el-form-item>
      <el-form-item prop="detailAddress" style="margin-bottom: 22px">
        <el-col :span="20" class="hospitalForm" style="margin-left: 140px">
          <el-amap-search-box
            ref="searchBox"
            v-model="branchHospitalForm.detailAddress"
            style="border-radius: 5px; border: 1px solid #F3F3F3;height: 36px;"
            :search-option="searchOption"
            :on-search-result="onSearchResult"
          />
        </el-col>
      </el-form-item>
      <el-form-item style="margin-bottom: 22px">
        <el-col :span="20" style="margin-left: 140px">
          <el-amap
            ref="map"
            :vid="'amapDemo'+index"
            :amap-manager="amapManager"
            :center="center"
            :zoom="zoom"
            :plugin="plugin"
            :events="events"
            class="amap-demo"
            style="height:500px;"
          >
            <el-amap-marker :position="center" />
          </el-amap>
        </el-col>
      </el-form-item>
      <el-form-item class="hospitalTable" label="可服务门店:" required style="margin-bottom: 22px">
        <el-col :span="20">
          <el-table
            :data="BindData"
            class="ele-table"
            :header-cell-style="handleCellStyle"
          >
            <el-table-column label="门店名称" prop="storeName" width="200" />
            <el-table-column label="门店地址" prop="address" width="200" />
            <el-table-column align="right" label="操作">
              <template slot-scope="scope">
                <el-button
                  style="color:red"
                  type="text"
                  @click="storeRemove(scope.row)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-button
            type="text"
            @click="addStore"
          >添加门店</el-button>
        </el-col>
      </el-form-item>
      <el-form-item label="适用商品:" required style="margin-bottom: 22px">
        <el-col :span="20" class="hospitalForm">
          <el-card shadow="never">
            <div class="ele-content">
              <div>
                <el-radio
                  v-model="branchHospitalForm.goodsType"
                  label="0"
                  @change="changeGoodsType"
                >支持部分商品</el-radio>
                <el-radio
                  v-model="branchHospitalForm.goodsType"
                  label="1"
                  @change="changeGoodsType"
                >支持全部商品</el-radio>
              </div>
            </div>
            <div v-if="branchHospitalForm.goodsType=='0'">
              <HospitalPartTable ref="hospitalPartTable" :store-goods-list="storeGoodsList" @changeStoreGoodsList="changeStoreGoodsList" />
              <!--              <el-table
                ref="storeGoods"
                :data="storeGoodsList"
                :header-cell-style="handleCellStyle"
                @selection-change="selectGoods"
              >
                <el-table-column type="selection" />
                <el-table-column label="商品">
                  <template slot-scope="scope">
                    <div class="cus-column-commodity">
                      <div class="left">
                        <TableColumnGoodsImage :src="scope.row.goodsPic" :img-size="'60px'" />
                      </div>
                      <div class="right">
                        <span style="display:inline-block;overflow: hidden;text-overflow:ellipsis;white-space: nowrap;width:80px">{{ scope.row.goodsName }}</span>
                      </div>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column label="推广分成" prop="customDivide" />
                <el-table-column label="医院" prop="isHospital">
                  <template slot-scope="scope">
                    <el-checkbox ref="isHospital" v-model="scope.row.isHospital" @change="changeIsHospital(scope.row)" />
                  </template>
                </el-table-column>
                <el-table-column label="护工或其他" prop="isCareWorker">
                  <template slot-scope="scope">
                    <el-checkbox ref="isCareWorker" v-model="scope.row.isCareWorker" @change="changeIsCareWorker(scope.row)" />
                  </template>
                </el-table-column>
              </el-table>-->
            </div>
            <div v-else-if="branchHospitalForm.goodsType=='1'">
              <HospitalAllTable ref="hospitalAllTable" :store-goods-list="storeGoodsList" @changeStoreGoodsList="changeStoreGoodsList" />
            </div>
            <!--            <div v-if="branchHospitalForm.goodsType=='1'">-->
            <!--              <el-table-->
            <!--                ref="storeGoods"-->
            <!--                :data="storeGoodsList"-->
            <!--                :header-cell-style="handleCellStyle"-->
            <!--              >-->
            <!--                <el-table-column label="商品">-->
            <!--                  <template slot-scope="scope">-->
            <!--                    <div class="cus-column-commodity">-->
            <!--                      <div class="left">-->
            <!--                        <TableColumnGoodsImage :src="scope.row.goodsPic" :img-size="'60px'" />-->
            <!--                      </div>-->
            <!--                      <div class="right">-->
            <!--                        <span style="display:inline-block;overflow: hidden;text-overflow:ellipsis;white-space: nowrap;width:80px">{{ scope.row.goodsName }}</span>-->
            <!--                      </div>-->
            <!--                    </div>-->
            <!--                  </template>-->
            <!--                </el-table-column>-->
            <!--                <el-table-column label="推广分成" prop="customDivide" />-->
            <!--                <el-table-column label="医院" prop="isHospital">-->
            <!--                  <template slot-scope="scope">-->
            <!--                    <el-checkbox ref="isHospital" v-model="scope.row.isHospital" @change="changeIsHospital(scope.row)" />-->
            <!--                  </template>-->
            <!--                </el-table-column>-->
            <!--                <el-table-column label="护工或其他" prop="isCareWorker">-->
            <!--                  <template slot-scope="scope">-->
            <!--                    <el-checkbox ref="isCareWorker" v-model="scope.row.isCareWorker" @change="changeIsCareWorker(scope.row)" />-->
            <!--                  </template>-->
            <!--                </el-table-column>-->
            <!--              </el-table>-->
            <!--            </div>-->
          </el-card>
        </el-col>
      </el-form-item>
      <div
        v-if="branchHospitalForm.detailAddress && BindData.length >0 && branchHospitalForm.goodsType && storeGoodsList.length>0 && getSelfPosition.length >0"
        style="margin-left: -15px;"
      >
        <el-form-item v-for="(item,index) in hospitalPositionList" :key="index">
          <hospital-position ref="hospitalPosition" :index="index" :self-position-list="getSelfPosition" />
        </el-form-item>
        <el-form-item>
          <el-button type="text" @click="addHospitalPosition">添加位置</el-button>
        </el-form-item>
      </div>
    </el-form>
    <!-- 商品门店列表 -->
    <add-hospital-store
      ref="AddHospitalStore"
      :visible="visible"
      @handelClose="handelClose"
      @handelAllSelect="handelAllSelect"
    />
    <el-button type="danger" circle icon="el-icon-close" style="position: absolute;top: -40px;right: 0" @click="delBranch(index)" />
  </div>
</template>

<script>
import { getCityList } from '@/api/auth'
import { getGoodsByIdList } from '@/api/customAPI/shop/hospitalManage'
import AddHospitalStore from '@/components/AddHospitalStore'
import HospitalPartTable from '@/components/HospitalPartTable'
import HospitalAllTable from '@/components/HospitalAllTable'
import HospitalPosition from '@/components/HospitalPosition'
import VueAMap from 'vue-amap'
const amapManager = new VueAMap.AMapManager()
export default {
  name: 'HospitalInfo',
  components: {
    AddHospitalStore,
    HospitalPartTable,
    HospitalAllTable,
    HospitalPosition
  },
  props: {
    index: {
      type: Number,
      required: true
    }
  },
  data() {
    const self = this
    return {
      visible: false,
      storeList: [],
      selection: [],
      BindData: [],
      id: null,
      storeGoodsList: [],
      selectIds: [],
      pca: {
        // 省区
        province: [],
        // 需要循环遍历的城区和市区
        city: [],
        area: [],
        // 最后拿到的 城区和市区 select 列表
        forCity: [],
        forArea: []
      },
      timeList: [],
      dMode_1: '',
      dMode_2: '',
      unchange: false,
      getSelfPosition: [],
      hospitalPositionList: [1],
      branchHospitalForm: {
        hospitalName: '',
        detailAddress: '',
        proportionalShare: '1',
        dModeTypeId: '',
        custom: '0',
        goodsType: null,
        selectGoodsIds: [],
        dMode: '',
        storeId: '',
        goodsId: '',
        provinceId: '',
        provinceName: '',
        cityId: '',
        cityName: '',
        areaId: '',
        areaName: '',
        provinceCode: '',
        cityCode: '',
        areaCode: ''
      },
      pattern: {
        hospitalName: [
          { required: true, max: 20, message: '请输入医院名称', trigger: 'blur' },
          { max: 20, message: '输入的字符超过20个', trigger: 'change' }
        ],
        expeditedCharge: [
          { required: true, message: '请输入加急费用', trigger: 'blur' }
        ],
        deliveryTime: [
          { required: true, message: '请输入送达时长', trigger: 'blur' }
        ],
        provinceCode: [
          { required: true, message: '请选择省份', trigger: 'change' }
        ],
        cityCode: [{ required: true, message: '请选择市', trigger: 'change' }],
        areaCode: [{ required: true, message: '请选择地区', trigger: 'change' }],
        detailAddress: [
          { required: true, message: '请输入医院具体地址', trigger: 'blur' }
        ]
      },
      /** 高德地图*/
      amapManager,
      zoom: 18,
      markers: [],
      searchOption: {
        city: '杭州',
        citylimit: false
      },
      center: [120.19, 30.26],
      events: {
        init: (o) => {
          o.getCity((result) => {
            console.log(result)
          })
        },
        moveend: () => {},
        zoomchange: () => {},
        click: (e) => {
          const geocoder = new window.AMap.Geocoder({
            radius: 1000,
            extensions: 'all'
          })
          this.center = [e.lnglat.lng, e.lnglat.lat]
          geocoder.getAddress(this.center, function(status, result) { // 根据坐标获取位置
            if (status === 'complete' && result.info === 'OK') {
              self.branchHospitalForm.detailAddress = result.regeocode.formattedAddress
              self.$refs.searchBox.keyword = self.branchHospitalForm.detailAddress
            }
          })
        }
      },
      plugin: [
        {
          showMarker: true, // 定位成功后在定位到的位置显示点标记，默认：true
          pName: 'Geolocation', // AMap-Geolocation 定位插件
          events: {
            init(o) {
              // getCurrentPosition 获取当前位置的方法
              // 注意 虽然进页面就会调用这个方法，但是data()中center要有默认值，不然会报错
              o.getCurrentPosition((status, result) => {
                if (result && result.position) {
                  const lng = result.position.lng
                  const lat = result.position.lat
                  if (self.center[0] == 120.19 && self.center[1] == 30.26 && !self.branchHospitalForm.detailAddress) {
                    self.center = [lng, lat]
                    self.loaded = true
                    self.zoom = 18
                    self.$nextTick()
                  }
                }
              })
            }
          }
        }
      ]
    }
  },
  created() {
    this.loadCity()
  },
  mounted() {

  },
  methods: {
    /** 编辑信息初始化*/
    inititalInfo(data) {
      if (data) {
        console.log(data)
        this.id = data.id
        this.branchHospitalForm.hospitalName = data.hospitalName
        // 省市区处理
        this.branchHospitalForm.provinceCode = data.provinceId
          ? data.provinceId
          : ''
        this.branchHospitalForm.cityCode = data.cityId
          ? data.cityId
          : ''
        this.branchHospitalForm.areaCode = data.villageId
          ? data.villageId
          : ''
        console.log(this.pca.province)
        this.branchHospitalForm.provinceId = this.pca.province.find(inf => {
          return inf.code === this.branchHospitalForm.provinceCode
        }).id
        this.branchHospitalForm.provinceName = this.pca.province.find(inf => {
          return inf.code === this.branchHospitalForm.provinceCode
        }).name
        this.pca.city.forEach(inf => {
          if (inf.pid === this.branchHospitalForm.provinceId) {
            this.pca.forCity.push(inf)
          }
        })
        this.branchHospitalForm.cityId = this.pca.city.find(inf => {
          return inf.code === this.branchHospitalForm.cityCode
        }).id
        this.branchHospitalForm.cityName = this.pca.city.find(inf => {
          return inf.code === this.branchHospitalForm.cityCode
        }).name
        this.pca.area.forEach(inf => {
          if (inf.pid === this.branchHospitalForm.cityId) {
            this.pca.forArea.push(inf)
          }
        })
        this.branchHospitalForm.areaId = this.pca.area.find(inf => {
          return inf.code === this.branchHospitalForm.areaCode
        }).id
        this.branchHospitalForm.areaName = this.pca.area.find(inf => {
          return inf.code === this.branchHospitalForm.areaCode
        }).name
        this.branchHospitalForm.detailAddress = data.addressDetail
        this.center = [data.lng, data.lat]
        this.BindData = data.storeList
        if (data.goodsType === 0) {
          this.branchHospitalForm.goodsType = '0'
        } else {
          this.branchHospitalForm.goodsType = '1'
        }
        if (this.BindData.length > 0) {
          this.goodsList = []
          const ids = this.BindData.map(info => info.id)
          getGoodsByIdList({
            storeIdList: ids,
            page: 1,
            limit: 0
          }).then(inf => {
            const _storeGoodsList = []
            inf.data.data.data.forEach(item => {
              _storeGoodsList.push({
                goodsId: item.goodsId,
                customDivide: item.customDivide,
                goodsName: item.goodsName,
                goodsPic: item.goodsPic,
                storeName: item.storeName,
                storeId: item.storeId,
                storeGoodsId: item.storeGoodsId,
                isHospital: 0,
                hospitalSend: [],
                isCareWorker: 0,
                carerSend: [],
                goodsType: item.goodsType
              })
            })
            const goodsIdList = data.goodsIds.split(',')
            const goodsList = data.goodsList

            this.selection = []
            this.getSelfPosition = []
            _storeGoodsList.forEach(item => {
              goodsList.forEach(info => {
                if (info.id == item.goodsId) {
                  console.log(info)
                  item.isHospital = info.hospitalShow.toString()
                  item.hospitalSend = info.hospitalSend.split(',')
                  item.isCareWorker = info.carerShow.toString()
                  item.carerSend = info.carerSend.split(',')
                  console.log(item)
                  if (info.hospitalShow == 1 && info.hospitalSend.split(',').includes('1')) {
                    this.getSelfPosition.push(item.storeGoodsId)
                  }
                }
              })

              goodsIdList.forEach(inf => {
                if (item.goodsId == inf) {
                  this.selection.push({
                    goodsId: item.goodsId,
                    hospitalShow: item.isHospital,
                    hospitalSend: item.hospitalSend,
                    carerShow: item.isCareWorker,
                    carerSend: item.carerSend
                  })
                }
              })
            })
            this.storeGoodsList = _storeGoodsList
            console.log(_storeGoodsList)
            this.$nextTick(() => {
              this.$refs.searchBox.keyword = this.branchHospitalForm.detailAddress
              if (this.branchHospitalForm.goodsType == '0') {
                this.$refs.hospitalPartTable.setData(goodsIdList, _storeGoodsList)
              }
            })
            if (data.locationVoList && data.locationVoList.length > 0) {
              this.hospitalPositionList = []
              data.locationVoList.forEach(inf => {
                this.hospitalPositionList.push(inf)
              })
              console.log(this.hospitalPositionList)
            }
            if (this.hospitalPositionList.length > 0) {
              this.$nextTick(() => {
                this.$refs.hospitalPosition.forEach((item, index) => {
                  item.setData(this.hospitalPositionList[index])
                })
              })
            }
          })
        }
      }
    },
    /** 删除服务门店 */
    storeRemove(row) {
      this.BindData.forEach((item, index) => {
        if (item.id === row.id) {
          this.BindData.splice(index, 1)
        }
      })
      if (this.BindData && this.BindData.length > 0) {
        this.getStoreGoodsList()
      } else {
        this.storeGoodsList = []
      }
    },
    handelReady($event) {
      this.BMap = $event.BMap
    },
    handelBMap($event) {
      this.center.lng = $event.point.lng
      this.center.lat = $event.point.lat
      this.branchHospitalForm.lng = $event.point.lng
      this.branchHospitalForm.lat = $event.point.lat
      if (this.BMap) {
        const geo = new this.BMap.Geocoder()
        geo.getLocation($event.point, res => {
          console.log(res)
          this.branchHospitalForm.detailAddress = res.address
          this.branchHospitalForm.provinceId = this.pca.province.find(inf => {
            return inf.name === res.addressComponents.province
          }).id
          this.branchHospitalForm.provinceCode = this.pca.province.find(inf => {
            return inf.name === res.addressComponents.province
          }).code
          this.pca.forCity = []
          this.pca.city.forEach(inf => {
            if (inf.pid === this.branchHospitalForm.provinceId) {
              this.pca.forCity.push(inf)
            }
          })
          this.branchHospitalForm.cityId = this.pca.forCity.find(inf => {
            if (res.addressComponents.province == res.addressComponents.city) {
              return inf.name == '市辖区'
            } else {
              return inf.name == res.addressComponents.city
            }
          }).id
          this.branchHospitalForm.cityCode = this.pca.forCity.find(inf => {
            if (res.addressComponents.province == res.addressComponents.city) {
              return inf.name == '市辖区'
            } else {
              return inf.name == res.addressComponents.city
            }
          }).code
          this.pca.forArea = []
          this.pca.area.forEach(inf => {
            if (inf.pid === this.branchHospitalForm.cityId) {
              this.pca.forArea.push(inf)
            }
          })
          console.log(this.pca.area)
          this.branchHospitalForm.areaId = this.pca.forArea.find(inf => {
            return inf.name == res.addressComponents.district
          }).id
          this.branchHospitalForm.areaCode = this.pca.forArea.find(inf => {
            return inf.name == res.addressComponents.district
          }).code
          this.branchHospitalForm.cityName = res.addressComponents.city
          this.branchHospitalForm.areaName = res.addressComponents.district
          this.branchHospitalForm.provinceName = res.addressComponents.province
          this.$nextTick(() => {
            this.provinceCity()
            this.addressName()
          })
          this.BindData = []
          this.storeGoodsList = []
        })
      }
    },
    handelAllSelect(list) {
      if (list.length > 0) {
        this.BindData = []
        this.storeGoodsList = []
        list.forEach(row => {
          this.BindData.push({
            id: row.id,
            storeName: row.storeName,
            address: row.address
          })
        })
        this.getStoreGoodsList()
        this.handelClose()
      }
    },
    changedAddress() {
      const vm = this
      if (vm.branchHospitalForm.address.province && vm.branchHospitalForm.address.city) {
        vm.province = vm.branchHospitalForm.address.province.code
        vm.city = vm.branchHospitalForm.address.city.code
        this.$refs.AddHospitalStore.getProvinceAndCity(vm.province, vm.city)
      }
      if (vm.branchHospitalForm.address.province) {
        vm.branchHospitalForm.provinceName = vm.branchHospitalForm.address.province.name
        vm.branchHospitalForm.cityName = ''
        vm.branchHospitalForm.villageName = ''
        vm.addressName()
      }
      if (vm.branchHospitalForm.address.city) {
        vm.branchHospitalForm.cityName = vm.branchHospitalForm.address.city.name
        vm.branchHospitalForm.villageName = ''
        vm.addressName()
      }
      if (vm.branchHospitalForm.address.village) {
        vm.branchHospitalForm.villageName = vm.branchHospitalForm.address.village.name
        vm.addressName()
      }
    },
    changeGoodsType() {
      this.storeGoodsList = []
      this.getStoreGoodsList()
    },
    /** 根据门店商品到院自提*/
    changeStoreGoodsList(list) {
      this.getSelfPosition = []
      console.log(list)
      if (list.length > 0) {
        list.forEach(item => {
          if (item.hospitalSend.includes('1')) {
            this.getSelfPosition.push(item.storeGoodsId)
          }
        })
        this.$nextTick(() => {
          this.$refs.hospitalPosition.forEach(item => {
            item.setData({
              id: null,
              locationName: null,
              startHour: null,
              endHour: null,
              remark: null,
              imgs: [],
              hpFlag: '0',
              inventoryFlag: '0',
              freeFlag: '0',
              adminPhone: '',
              hospitalSelfLocationEquList: []
            })
          })
        })
      }
    },
    /** 添加自提位置*/
    addHospitalPosition() {
      this.hospitalPositionList.push(1)
      this.$nextTick(() => {
        this.$refs.hospitalPosition.forEach((item, index) => {
          if (index == (this.hospitalPositionList.length - 1)) {
            item.setData({
              id: null,
              locationName: null,
              startHour: null,
              endHour: null,
              remark: null,
              imgs: [],
              hpFlag: '0',
              inventoryFlag: '0',
              freeFlag: '0',
              adminPhone: '',
              hospitalSelfLocationEquList: []
            })
          }
        })
      })
    },
    /** 门店商品选择 */
    /* selectGoods(v) {
      console.log(v)
      this.selection = []
      v.forEach(item => {
        this.selection.push({
          goodsId: item.goodsId,
          hospitalShow: item.isHospital ? 1 : 0,
          carerShow: item.isCareWorker ? 1 : 0
        })
      })
      console.log(this.selection)
    },*/
    /** 添加门店 */
    addStore() {
      if (this.branchHospitalForm.provinceCode && this.branchHospitalForm.cityCode) {
        this.visible = true
        if (this.BindData.length > 0) {
          this.$refs.AddHospitalStore.setSelectList(this.BindData)
        }
        this.$refs.AddHospitalStore.resetForm()
      } else {
        this.$message.error('请选择医院省份和城市')
      }
    },
    /** 根据门店列表获取商品列表*/
    getStoreGoodsList() {
      if (this.BindData.length > 0) {
        this.storeGoodsList = []
        const ids = this.BindData.map(info => info.id)
        getGoodsByIdList({
          storeIdList: ids,
          page: 1,
          limit: 0
        }).then(inf => {
          inf.data.data.data.forEach(item => {
            this.storeGoodsList.push({
              goodsId: item.goodsId,
              customDivide: item.customDivide,
              goodsName: item.goodsName,
              goodsPic: item.goodsPic,
              storeId: item.storeId,
              storeGoodsId: item.storeGoodsId,
              isHospital: 0,
              isCareWorker: 0,
              hospitalSend: [],
              carerSend: [],
              goodsType: item.goodsType
            })
          })
        })
      }
    },
    /** 区 */
    handelArea(v) {
      console.log(v, '区id')
      if (v) {
        this.branchHospitalForm.areaName = this.pca.area.find(inf => {
          return inf.code === v
        }).name
        this.branchHospitalForm.areaId = this.pca.area.find(inf => {
          return inf.code === v
        }).code
        this.addressName()
      }
    },
    /** 市 */
    handelCity(v) {
      if (v) {
        this.pca.forArea = []
        this.branchHospitalForm.areaName = ''
        this.branchHospitalForm.areaId = ''
        this.branchHospitalForm.areaCode = null
        this.BindData = []
        this.storeGoodsList = []
        const cityInfo = this.$XEUtils.find(this.pca.city, item => item.code === v)
        this.pca.area.forEach(inf => {
          if (inf.pid === cityInfo.id) {
            this.pca.forArea.push(inf)
          }
        })
        this.branchHospitalForm.cityName = this.pca.city.find(inf => {
          return inf.code === v
        }).name
        this.branchHospitalForm.cityId = this.pca.city.find(inf => {
          return inf.code === v
        }).code
        this.addressName()
        if (this.branchHospitalForm.provinceCode && this.branchHospitalForm.cityCode) {
          this.$refs.AddHospitalStore.getProvinceAndCity(this.branchHospitalForm.provinceCode, this.branchHospitalForm.cityCode)
        }
      }
    },
    /** 省 */
    handelProvince(v) {
      this.pca.forCity = []
      this.pca.forArea = []
      this.branchHospitalForm.cityId = ''
      this.branchHospitalForm.areaId = ''
      this.branchHospitalForm.cityCode = null
      this.branchHospitalForm.areaCode = null
      this.BindData = []
      this.storeGoodsList = []
      if (v) {
        const provinceInfo = this.$XEUtils.find(this.pca.province, item => item.code === v)
        this.pca.city.forEach(inf => {
          if (inf.pid === provinceInfo.id) {
            this.pca.forCity.push(inf)
          }
        })

        this.branchHospitalForm.cityName = ''
        this.branchHospitalForm.areaName = ''
        this.branchHospitalForm.detailAddress = ''
        this.branchHospitalForm.provinceName = this.pca.province.find(inf => {
          return inf.code === v
        }).name
        this.branchHospitalForm.provinceId = this.pca.province.find(inf => {
          return inf.code === v
        }).id
        this.branchHospitalForm.provinceCode = this.pca.province.find(inf => {
          return inf.code === v
        }).code
        this.addressName()
      }
    },
    /** 地址根据省市区变化*/
    addressName() {
      this.branchHospitalForm.detailAddress = ''
      this.branchHospitalForm.detailAddress =
        this.branchHospitalForm.provinceName +
        this.branchHospitalForm.cityName +
        this.branchHospitalForm.areaName
    },
    /** 修改时初始化 */
    provinceCity() {
      this.pca.forCiry = []
      this.pca.forArea = []
      this.pca.city.forEach(inf => {
        if (inf.pid === this.branchHospitalForm.provinceCode) {
          this.pca.forCiry.push(inf)
        }
      })
      this.pca.area.forEach(inf => {
        if (inf.pid === this.branchHospitalForm.cityCode) {
          this.pca.forArea.push(inf)
        }
      })
    },
    /** 初始地址 */
    loadCity() {
      getCityList()
        .then(result => {
          this.pca.province = []
          this.pca.city = []
          this.pca.area = []
          result.data.data.forEach(inf => {
            this.pca.province.push(inf.province)
            inf.cityRegion.forEach(reg => {
              this.pca.city.push(reg.city) // 拿市区
              reg.village.forEach(vil => {
                this.pca.area.push(vil) // 拿城区
              })
            })
          })
          /* this.inititalInfo()*/
        })
        .catch(err => {
          console.log(err)
        })
    },
    /** 表格头部样式*/
    handleCellStyle({ row, column, rowIndex, columnIndex }) {
      if (rowIndex === 0) {
        return 'background:#f3f3f3'
      } else {
        return ''
      }
    },
    /** 商品列表勾选状态变化*/
    /* changeIsHospital(data) {
      if (this.selection.length > 0) {
        const list = []
        this.selection.forEach(item => {
          list.push(item.goodsId)
          if (item.goodsId == data.goodsId) {
            item.hospitalShow = data.isHospital ? 1 : 0
          }
        })
        if (!list.includes(data.goodsId)) {
          this.storeGoodsList.forEach(item => {
            if (item.goodsId == data.goodsId) {
              item.isHospital = false
            }
          })
          return this.$message.error('选中该商品后再勾选医院推广')
        }
      } else {
        data.isHospital = false
        return this.$message.error('请先选中该商品')
      }
    },*/
    /** 商品列表勾选状态变化*/
    /* changeIsCareWorker(data) {
      if (this.selection.length > 0) {
        const list = []
        this.selection.forEach(item => {
          list.push(item.goodsId)
          if (item.goodsId == data.goodsId) {
            item.carerShow = data.isCareWorker ? 1 : 0
          }
        })
        if (!list.includes(data.goodsId)) {
          this.storeGoodsList.forEach(item => {
            if (item.goodsId == data.goodsId) {
              item.isCareWorker = false
            }
          })
          return this.$message.error('选中该商品后再勾选医院推广')
        }
      } else {
        data.isCareWorker = false
        return this.$message.error('请先选中该商品')
      }
    },*/
    handelClose() {
      this.visible = false
    },
    delBranch(index) {
      this.$emit('delBranch', index)
    },
    getData() {
      if (!this.branchHospitalForm.hospitalName) {
        this.$message.error('第' + (this.index + 1) + '个医院分院名称不能为空')
        return
      }
      if (!this.branchHospitalForm.provinceCode) {
        this.$message.error('第' + (this.index + 1) + '个医院分院所在省份不能为空')
        return
      }
      if (!this.branchHospitalForm.cityCode) {
        this.$message.error('第' + (this.index + 1) + '个医院分院所在城市不能为空')
        return
      }
      if (!this.branchHospitalForm.areaCode) {
        this.$message.error('第' + (this.index + 1) + '个医院分院所在地区不能为空')
        return
      }
      if (!this.branchHospitalForm.detailAddress) {
        this.$message.error('第' + (this.index + 1) + '个医院分院所在具体地址不能为空')
        return
      }
      if (this.center[0] == 120.19 && this.center[1] == 30.26) {
        this.$message.error('请重新选择第' + (this.index + 1) + '个医院分院区域的坐标点')
        return
      }
      if (this.BindData.length === 0) {
        this.$message.error('请单击第' + (this.index + 1) + '个医院分院未添加服务门店')
        return
      }
      /*   this.$nextTick(() => {*/

      if (this.branchHospitalForm.goodsType == 0) {
        if (this.$refs.hospitalPartTable.getData()) {
          this.selection = this.$refs.hospitalPartTable.getData()
        }
      } else {
        if (this.$refs.hospitalAllTable.getData()) {
          this.selection = this.$refs.hospitalAllTable.getData()
        }
      }
      console.log(this.selection)
      if (this.selection.length === 0) {
        this.$message.error('请单击第' + (this.index + 1) + '个医院分院未添加适用商品')
        return
      }
      if (this.selection.length > 0) {
        for (let i = 0; i < this.selection.length; i++) {
          if (this.selection[i].carerShow == 0 && this.selection[i].hospitalShow == 0) {
            this.$message.error('第' + (this.index + 1) + '个医院分院商品至少勾选一个推广类型')
            return
          }
          if (this.selection[i].hospitalShow == 1 && !this.selection[i].hospitalSend) {
            this.$message.error('第' + (this.index + 1) + '个医院分院商品勾选医院时，至少选择一个医院配送方式')
            return
          }
          if (this.selection[i].carerShow == 1 && !this.selection[i].carerSend) {
            this.$message.error('第' + (this.index + 1) + '个医院分院商品勾选护工或其他时，至少选择一个护工配送方式')
            return
          }
        }
      }
      const hospitalSelfLocation = []
      if (this.getSelfPosition > 0 && this.hospitalPositionList.length > 0) {
        this.$refs.hospitalPosition.forEach(item => {
          if (item.getData()) {
            hospitalSelfLocation.push(item.getData())
          }
        })
      }
      const storeList = []
      const storeIds = []
      this.BindData.forEach(item => {
        storeList.push({
          storeId: item.id,
          sendType: 1
        })
        storeIds.push(item.id)
      })

      const data = {
        hospitalName: this.branchHospitalForm.hospitalName,
        provinceId: this.branchHospitalForm.provinceCode,
        cityId: this.branchHospitalForm.cityCode,
        villageId: this.branchHospitalForm.areaCode,
        province: this.branchHospitalForm.provinceName,
        city: this.branchHospitalForm.cityName,
        village: this.branchHospitalForm.areaName,
        addressDetail: this.branchHospitalForm.detailAddress,
        storeIds: storeIds.toString(),
        storeData: storeList,
        goodsType: this.branchHospitalForm.goodsType === '0' ? 0 : 1,
        goodsData: this.selection,
        lng: this.center[0],
        lat: this.center[1]
      }
      if (this.getSelfPosition > 0 && this.hospitalPositionList.length == 1) {
        data['hospitalSelfLocation'] = hospitalSelfLocation
      }
      if (this.id) {
        data['id'] = this.id
      }
      return data
      /* })*/
    },
    /** 点击搜索后触发的事件 */
    onSearchResult(pois) {
      if (pois.length > 0) {
        pois.forEach(item => {
          if (this.$refs.searchBox.keyword == item.name) {
            this.center = [item.lng, item.lat]
          } else {
            // 这边类似模糊查询 会返回一个数组，我就直接取第一个值了。
            this.center = [pois[0].lng, pois[0].lat]
          }
        })
      }
      this.branchHospitalForm.detailAddress = this.$refs.searchBox.keyword
    }
  }
}
</script>

<style lang="scss" scoped>
.time {
  ::v-deep .el-form-item__error {
    margin-left: 0 !important;
  }
}
</style>
